<template>
  <BaseForm v-model="formDataModel" @close="$emit('callBack')" />

</template>

<script lang="tsx" setup>
import type { FormInstance } from 'element-plus'
import PurchaseOrderInfoApi from '/@/api/webPath/PurchaseOrderInfoApi.ts';
import { translate } from "/@/i18n";
import FORM_FIELD_TYPE from "/@/constants/FormFieldType.ts";
import PurchaseOrderInfoDetailApi from '~/src/api/webPath/PurchaseOrderInfoDetailApi';
defineOptions({
  name: 'PurchaseOrderInfoEdit',
})
const props = defineProps({
  modelValue: {

  }
})
const emit = defineEmits(['fetch-data', 'callBack'])

const formRef = ref<FormInstance>();
const title = ref<string>('');
const dialogFormVisible = ref<boolean>(false);



const formDataModel = ref({
  columnCount: 1,
  expandLimit: 6,
  autoFit: false,
  columnWidth: '300px',
  labelPosition: 'right',
  labelWidth: '100px',
  modelData: {},  //表单参数值 初始化

  callback: async (any: object) => {  //表单执行成功后回调
    //完成数据提交
    const { message } = await PurchaseOrderInfoApi.save(any)
    $baseMessage(message, 'success', 'vab-hey-message-success');
    dialogFormVisible.value = false

    //事件及数据推送到上一层父类
    emit('callBack', any); //把表单数据推送到上一层
  },
  items: [  //表单item数据初始化
    //           {
    //     label: "收货人",
    //     field: "deliveryName",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "收货电话",
    //     field: "deliveryTelephone",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "详细收货地址",
    //     field: "deliveryAddress",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    {
      field: "list",
      type: FORM_FIELD_TYPE.EDITABLETABLE,
      modelData: {
        data: [],
        columns: [
          {
            field: "id",
            hidden: true,
            width: 200
          },
          {
            label: "区域名称",
            field: "areaName",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: areaNameList,
            width: 200
          },
          {
            label: "类型",
            field: "type",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: typeList,
            async callback(v) {
              console.log("vvvvv", v)
              // deviceNameList.value =  
              console.log("pidList", pidList.value)
              const typeItem = pidList.value.find(x => x.value === v)
              const pid = typeItem && typeItem.pid
              console.log("current pid", pid)
              deviceNameList.value = (await useSysDataPidList("three_proof_equipment_type", pid)).value
              console.log("deviceNameList", deviceNameList.value)
              console.log("type value", v)
            },
            width: 200
          },
          {
            label: "设备名称",
            field: "deviceName",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: deviceNameList,
            width: 200
          },
          {
            label: "设备数量",
            field: "deviceNum",
            width: 200
          },
          {
            label: "设备状态",
            field: "deviceStatus",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: [
              {
                label: "完好",
                value: "1"
              },
              {
                label: "异常",
                value: "2"
              }
            ],
            width: 200
          },
          {
            label: "捕获虫害情况",
            field: "captureStatus",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: [
              {
                label: "无",
                value: "1"
              },
              {
                label: "有",
                value: "2"
              }
            ],
            width: 150
          },
          {
            label: "检查人",
            field: "operator",
            type: FORM_FIELD_TYPE.READONLY,
            defaultValue: userName,
          }
        ]
      }

    },
    {
      label: "订单编号",
      field: "orderCode",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "订单名称",
      field: "orderName",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    //   {
    //     label: "所属公司",
    //     field: "companyId",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "采购人id",
    //     field: "purchaserId",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    // {
    //   label: "采购人名称",
    //   field: "purchaserName",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUT,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    {
      label: "采购批次",
      field: "purchaserBatch",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    //   {
    //     label: "创建人",
    //     field: "creator",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "修改人",
    //     field: "reviser",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "排序",
    //     field: "sort",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUTNUMBER,
    //     defaultValue: 0,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    {
      label: "采购清单详情",
      field: "",
      type: FORM_FIELD_TYPE.UPLOAD_IMPORT,
      config: {
        // doUpload: PurchaseOrderInfoDetailApi.excelImport,
        // doBatchSave: PurchaseOrderInfoDetailApi.batchSave
        doUpload: PurchaseOrderInfoApi.excelImport,
        doBatchSave: PurchaseOrderInfoApi.batchSave
      },
      isShow: () => {
        console.log("moelvalue.query", props.modelValue.query)
        return Object.keys(props.modelValue!.query).length === 0
      },
    },
    {
      label: "采购清单详情",
      type: FORM_FIELD_TYPE.TABLE,
      isShow: () => {
        console.log("moelvalue.query", props.modelValue.query)
        return Object.keys(props.modelValue!.query).length > 0
      },
      modelData: {
        descriptionTitle: "采购清单详情",
        border: true,
        pagination: true,
        inline: true,
        selectable: false,
        isFullscreen: false,
        minWidth: '200px',
        data: {    //table  初始化
          current: 1,
          size: 10,
          total: 100,
          data: []
        },
        requestData: { api: PurchaseOrderInfoDetailApi.getPageData, query: { orderCode: props.modelValue!.query.orderCode } },   //数据源 参数有效性： 'steady|transient'
        columns: [
          //    {
          //     label: "id",
          //     field: "id",
          //     width:100,
          //       },
          // {
          //   label: "排序",
          //   field: "sort",
          //   width:120,
          //     },

          {
            label: "关联采购单",
            field: "orderId",
            width: 120,
          },
          {
            label: "餐次",
            field: "mealTimes",
            width: 180,
          },
          {
            label: "班组",
            field: "teamsGroups",
            width: 180,
          },
          {
            label: "食材名称",
            field: "foodName",
            width: 180,
          },
          {
            label: "采购数量",
            field: "num",
            width: 150,
          },
          {
            label: "单位",
            field: "unit",
            width: 180,
          },
          {
            label: "单价",
            field: "unitPrice",
            width: 150,
          },
          {
            label: "总价",
            field: "totalPrice",
            width: 150,
          },

          {
            label: "创建人",
            field: "creatorName",
            width: 180,
          },
          {
            label: "修改人",
            field: "reviserName",
            width: 180,
          },
          {
            label: "供应商",
            field: "supplierName",
            width: 200,
          },
          {
            label: "创建时间",
            field: "create_time",
            width: 200,
          },
          {
            label: "更新时间",
            field: "update_time",
            width: 200,
          },
        ],


        actions: [
        ]

      }
    }

  ],
})






//页面数据加载
onMounted(async () => {

  //初始化数据model
  if (props.modelValue.query) { //跳转过来时候有参数
    if (props.modelValue.query.purchaseOrderInfoId) {  //id存在则为编辑，加载网络数据，如上级页面带参数手动覆盖

      const { data } = await PurchaseOrderInfoApi.getDetail({
        purchaseOrderInfoId: props.modelValue.query.purchaseOrderInfoId   //从路由中获取参数
      })
      formDataModel.value.modelData = data;
    } else {
      //新增时候则用父类页面传参默认填充可根据业务修改
      for (const key in props.modelValue.query) {
        formDataModel.value.modelData[key] = props.modelValue.query[key]
      }
    }
  }
})


//方法暴露区域  todo
defineExpose({

})

</script>
